package com.ymatou.iapi.csim.testcase;

import java.text.SimpleDateFormat;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.csim.parameter.GetCurrentSessionListBean;
import com.ymatou.iapi.csim.parameter.GetCurrentSessionListResp.SessionDto;
import com.ymatou.iapi.csim.service.GetCurrentSessionListCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CsimCallService;
import com.ymttest.database.model.ymt_cssession;
import com.ymttest.database.model.ymt_onlinecs;
import com.ymttest.database.sqlwapper.CSIM.ymt_cssessionWapper;
import com.ymttest.database.sqlwapper.CSIM.ymt_onlinecsWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;
/**
*获取当前在线客服的会话列表
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetCurrentSessionList{
	private static GetCurrentSessionListBean bean;
	private static GetCurrentSessionListCall call;
	private CsimCallService service=new CsimCallService();
	private Integer csId=Integer.parseInt(EnvSetup.getData("csId").toString());
	private Integer csId_seller=Integer.parseInt(EnvSetup.getData("csId_seller").toString());
	private Integer userId=Integer.parseInt(EnvSetup.getData("userId_IM").toString());
	private Integer sellerId1=Integer.parseInt(EnvSetup.getData("autotest_seller1").toString());
	private static ymt_cssessionWapper csSessionWapper=new ymt_cssessionWapper();
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取当前在线客服的会话列表");
	}

	@Before
	public void caseUp() {
		bean=new GetCurrentSessionListBean();
		call=new GetCurrentSessionListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetCurrentSessionList_1() {
		Logger.start(true,"获取当前在线客服的会话列表-买家客服对买家");
		try {
			//csIdA上线
			ymt_onlinecs onlinecs=service.getOnlineCS(csId);
			//csIdA 主动会话
			ymt_cssession csSession=service.getOpenSession(csId,userId);
			bean.setCsId(csId);
			call.setData(bean);
			call.callService();
			
			verifyReturn();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetCurrentSessionList_2() {
		Logger.start(true,"获取当前在线客服的会话列表-买手客服对买手");
		try {
			//csIdA上线
			ymt_onlinecs onlinecs=service.getOnlineCS_Seller(csId_seller);
			//清楚历史记录
			csSessionWapper.deleteByUserId(MapUtil.hashMap("UserId",sellerId1));
			//csIdA 主动会话
			ymt_cssession csSession=service.getOpenSession(csId_seller,sellerId1);
			
			bean.setCsId(csId_seller);
			call.setData(bean);
			call.callService();

			verifyReturn();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetCurrentSessionList_3() {
		Logger.start(true,"获取当前在线客服的会话列表-验证会话关闭");
		try {
			//csIdA上线
			ymt_onlinecs onlinecs=service.getOnlineCS(csId);
			//csIdA 主动会话
			ymt_cssession csSession=service.getOpenSession(csId,userId);
			csSession.setStatus(1);
			csSessionWapper.updateByPrimaryKey(csSession);
			bean.setCsId(csId);
			call.setData(bean);
			call.callService();
			
			Logger.verifyEquals(false, call.getData().getResult().contains(csSession), "验证会话关闭不显示");
			
			verifyReturn();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetCurrentSessionList_4() {
		Logger.start(true,"获取当前在线客服的会话列表-验证csId为空");
		try {
			//csIdA上线
			ymt_onlinecs onlinecs=service.getOnlineCS(csId);
			//csIdA 主动会话
			ymt_cssession csSession=service.getOpenSession(csId,userId);
			csSession.setStatus(1);
			csSessionWapper.updateByPrimaryKey(csSession);
			
		//	bean.setCsId(csId);
			call.setData(bean);
			call.callService();
			
			Logger.verifyEquals(0, call.getData().getResult().size(), "验证返回的Result为空");
	
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	 private void verifyReturn() throws InterruptedException
	 {
		//验证数据库
			List<ymt_cssession>  cssessionList=csSessionWapper.selectAllBy(MapUtil.hashMap("CSId",bean.getCsId(),"Status",0));
			List<SessionDto> sessionList=call.getData().getResult();
			for(int i=0;i<sessionList.size();i++)
			{
				 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				 String launchTime = formatter.format( cssessionList.get(i).getLaunchTime());
				 String beginTime = formatter.format( cssessionList.get(i).getBeginTime());
				 
				Logger.verifyEquals(sessionList.get(i).getSessionId(), cssessionList.get(i).getId(), "验证第"+(i+1)+"个会话SessionId");
				Logger.verifyEquals(sessionList.get(i).getSessionNumber(), cssessionList.get(i).getSessionNumber(), "验证第"+(i+1)+"个会话SessionNumber");
				Logger.verifyEquals(sessionList.get(i).getUnreadMessageCount(), cssessionList.get(i).getUnReadCount(), "验证第"+(i+1)+"个会话SessionId");
				Logger.verifyEquals(sessionList.get(i).getToUserId(), cssessionList.get(i).getUserId(), "验证第"+(i+1)+"个会话ToUserId");
				Logger.verifyEquals(sessionList.get(i).getToUserLogo(), cssessionList.get(i).getUserLogo(), "验证第"+(i+1)+"个会话ToUserLogo");
				Logger.verifyEquals(sessionList.get(i).getToUserName(), cssessionList.get(i).getUserName(), "验证第"+(i+1)+"个会话UserName");
				Logger.verifyEquals(sessionList.get(i).getLaunchTime(), launchTime, "验证第"+(i+1)+"个会话LaunchTime");
				Logger.verifyEquals(sessionList.get(i).getBeginTime(), beginTime, "验证第"+(i+1)+"个会话BeginTime");
				Logger.verifyEquals(sessionList.get(i).getFromPlatform(), cssessionList.get(i).getFromPlatform(), "验证第"+(i+1)+"个会话FromPlatform");
				Logger.verifyEquals(sessionList.get(i).getOpenType(), cssessionList.get(i).getOpenType(), "验证第"+(i+1)+"个会话OpenType");
				Logger.verifyEquals(sessionList.get(i).getMark(), cssessionList.get(i).getMark()==null?0:cssessionList.get(i).getMark(), "验证第"+(i+1)+"个会话Mark");
			}
	 }
}
